package com.somnus.demo.androiddatabingdemo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.somnus.demo.androiddatabingdemo.BR;
import com.somnus.demo.androiddatabingdemo.R;
import com.somnus.demo.androiddatabingdemo.bean.MovieBean;
import com.somnus.demo.androiddatabingdemo.databinding.ItemMovieBinding;

import java.util.List;

/**
 * Created by Somnus on 2017/3/14.
 *
 * @Description:
 */

public class RecycerViewAdapter extends RecyclerView.Adapter<RecycerViewAdapter.BindingViewHolder> {

    private List<MovieBean> mMovieBeenList;
    private Context mContext;

    public RecycerViewAdapter(Context context, List<MovieBean> list) {
        mContext = context;
        mMovieBeenList = list;
    }

    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        ItemMovieBinding itemMovieBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_movie, parent, false);
        BindingViewHolder holder = new BindingViewHolder(itemMovieBinding.getRoot());
        holder.setBinding(itemMovieBinding);
        return holder;
    }

    @Override
    public void onBindViewHolder(BindingViewHolder holder, int position) {
        MovieBean movieBean = this.mMovieBeenList.get(position);
        holder.mItemMovieBinding.setVariable(BR.movie,movieBean);
        holder.mItemMovieBinding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return mMovieBeenList.size();
    }

    public class BindingViewHolder extends RecyclerView.ViewHolder {
        private ItemMovieBinding mItemMovieBinding;

        public BindingViewHolder(View itemView) {
            super(itemView);
        }

        public ItemMovieBinding getItemMovieBinding() {
            return mItemMovieBinding;
        }

        public void setItemMovieBinding(ItemMovieBinding itemMovieBinding) {
            mItemMovieBinding = itemMovieBinding;
        }


        public void setBinding(ItemMovieBinding binding) {
            mItemMovieBinding = binding;
        }
    }

}
